<?php
/**
 * @version			$Id: gnudoors.php 62 2010-08-25 12:24:56Z lowcosta@gmail.com $
 * @author			Richard Asp
 * @package			Joomla
 * @subpackage		gnudoor
 * @copyright		(C) 2010 gnudoor LLC
 * @license      GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
 */
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );
jimport( 'joomla.application.component.model' );

class GnudoorModelGnudoors extends JModel
{
	var $_data = null;
	var $_total = null;
	var $_pagination = null;
	var $_attrib;
	
	function __construct(){

 	     parent::__construct(); 

 	     global $mainframe, $option;
 
        $filter_order     = $mainframe->getUserStateFromRequest(  $option.'filter_order', 'filter_order', 'dateDue', 'cmd' );
        $filter_order_Dir = $mainframe->getUserStateFromRequest( $option.'filter_order_Dir', 'filter_order_Dir', 'asc', 'word' );
        $active_tab = $mainframe->getUserStateFromRequest( $option.'active_tab', 'active_tab', 'tab_one', 'word' );
        
        $this->setState('filter_order', $filter_order);
        $this->setState('filter_order_Dir', $filter_order_Dir);
        $this->setState('active_tab', $active_tab);
        $this->setState('steptask', $task);
        
	    $application = JFactory::getApplication() ; 
	    $config = JFactory::getConfig() ; 

	    $limitstart = JRequest::getInt( 'limitstart', 0 );
	    $limit = $application->getUserStateFromRequest( 'global.list.limit', 
			'limit', $config->getValue('config.list_limit'), 'int' );        
  
	    $this->setState('limitstart', $limitstart); 
	    $this->setState('limit', $limit); 
	}
	
	function _loadData() {
		if (empty($this->_data) && empty($this->_total)) {
           	 
		    $query = $this->_buildQuery() ;
		    $query .= $this->_buildContentOrderBy();
	        $this->_db->setQuery($query); 	
	        $this->_data = $this->_db->loadObjectList(); 
	        $this->_total = count( $this->_data ) ; 
		} 

    	return $this->_data ; 
    }
	
    function _buildContentOrderBy() {
        global $mainframe, $option;
 
        $orderby = '';
        $filter_order     = $this->getState('filter_order');
        $filter_order_Dir = $this->getState('filter_order_Dir');
 
        /* Error handling is never a bad thing*/
        if(!empty($filter_order) && !empty($filter_order_Dir) ){
        	$orderby = ' ORDER BY '.$filter_order.' '.$filter_order_Dir;
        }
 
        return $orderby;
	}
    
	function _buildQuery() {

        $user =& JFactory::getUser();
        $usr_id = $user->get('id');    
        $active_tab = $this->getState('active_tab');
		$query = ' SELECT * '
			. ' FROM #__gnudoor '
			. ' WHERE userid = '. $usr_id
			;
		$querys = ' AND status =' ;

		switch ($active_tab) {
		    case 'tab_one':
		        $querys .= 1;
		        break;
		    case 'tab_two':
		        $querys .= 2;
		        break;
		    case 'tab_three':
		        $querys .= 3;
		        break;
		    case 'tab_four':
		        $querys .= 4;
		        break;
		    case 'tab_five':
		        $querys .= 5;
		        break;
		    case 'tab_six':
		        $querys .= 6;
		        break;
		    default:
		        $querys = '';
		}
		$query .= $querys;
		return $query;
	}

	/**
	 * Retrieves the data
	 * @return array Array of objects containing the data from the database
	 */
	function getData() {
   	    $this->_loadData() ; 
    
	    $limitstart = $this->getState('limitstart');
	    $limit = $this->getState('limit');

	    if (isset($this->_data)) {
		    return array_slice( $this->_data, $limitstart, $limit );		
		}
		return;
	}

	function getTotal() {
       	return $this->_total; 
	}
	function getPagination() {
		$this->_loadData() ;

		if (empty($this->_pagination)) 
		{ 
	        jimport('joomla.html.pagination'); 
	
	        $limitstart = $this->getState('limitstart');
	        $limit = $this->getState('limit');
	        $total = $this->getTotal();
	
	        $this->_pagination = new JPagination( $total, $limitstart, $limit ); 
		} 
		return $this->_pagination; 
	} 

	function store() {
		$row =& $this->getTable();
		$data = JRequest::get( 'post' );

		// Bind the form fields to the Gnudoor table
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// Make sure the Gnudoor record is valid
		if (!$row->check()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// Store the web link table to the database
		if (!$row->store()) {
			$this->setError( $row->getErrorMsg() );
			return false;
		}

		return true;
	}
	
	function getAttributeArray() {
		$query = ' SELECT attribute, text '
			. ' FROM #__gnudoorproperties ';
	    $this->_db->setQuery($query);
	    $this->_attrib = $this->_db->loadObjectList();
	    foreach ($this->_attrib as $item) {
	    	$array[$item->attribute] = $item->text;
	    }
	    return $array;
	
	}
	function getStatus() {
		$query = ' SELECT * '
			. ' FROM #__gnudoorstatus ';
	    $this->_db->setQuery($query);
	    $this->_attrib = $this->_db->loadObjectList();
		    foreach ($this->_attrib as $item) {
	    	$array[$item->id] = $item->sel;
	    }	    
	    return $array;
	
	}

	function getProviderName($bidid) {
		$query = 	' SELECT p.name, b.providerid '
				.	' FROM #__users AS p JOIN #__gnudoorbids as b '
				.	' WHERE p.id = b.providerid AND b.id=' . $bidid
				;
			    $this->_db->setQuery($query);
	    return $this->_db->loadObject();
	}

	function getGnudoorGroup() {
		$query = ' SELECT userid, gnudoor_group '
			. ' FROM #__gnudoor_acl '
			;
		$this->_db->setQuery($query);
	    return $this->_db->loadObjectList();
		
	}

	function getArticleID($articlealias = null) {
			$query = ' SELECT id '
				. ' FROM #__content '
				. ' WHERE alias="' . $articlealias . '"'
				;
			$this->_db->setQuery($query);
		    return $this->_db->loadObject();
		
	}
}

?>
